package org.tote.model;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "mail_message")
public class MailMessageTemplate {

	@Id
	@Column(name = "type")
	@Enumerated(value = EnumType.STRING)
	private MailMessageType type;
	
	@Column(name = "subject")
	private String subject;
	
	@Column(name = "body")
	private String body;
	
	@Transient
	private Map<String, String> parameters = new HashMap<String, String>();

	/**
     * @return the type
     */
    public MailMessageType getType() {
    	return type;
    }

	/**
     * @param type the type to set
     */
    public void setType(MailMessageType type) {
    	this.type = type;
    }

	/**
     * @return the subject
     */
    public String getSubject() {
    	return subject;
    }

	/**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
    	this.subject = subject;
    }

	/**
     * @return the body
     */
    public String getBody() {
    	return body;
    }

	/**
     * @param body the body to set
     */
    public void setBody(String body) {
    	this.body = body;
    }
    
    /**
     * Adds parameter to parameters list of message
     * @param name - name of parameter
     * @param value - value of parameter
     * @return - the same object of MailMessageTemplate to add parameters in chain 
     */
    public MailMessageTemplate addParameter(String name, String value) {
    	this.parameters.put(name, value);
    	return this;
    }
    
    public Map<String, String> getParameters() {
    	return parameters;
    }

	/* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
	    return "MailMessage [type=" + type + ", subject=" + subject + ", body="
	            + body + "]";
    }

	/* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
	    final int prime = 31;
	    int result = 1;
	    result = prime * result + ((body == null) ? 0 : body.hashCode());
	    result = prime * result + ((subject == null) ? 0 : subject.hashCode());
	    result = prime * result + ((type == null) ? 0 : type.hashCode());
	    return result;
    }

	/* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
	    if (this == obj) {
		    return true;
	    }
	    if (obj == null) {
		    return false;
	    }
	    if (!(obj instanceof MailMessageTemplate)) {
		    return false;
	    }
	    MailMessageTemplate other = (MailMessageTemplate) obj;
	    if (body == null) {
		    if (other.body != null) {
			    return false;
		    }
	    } else if (!body.equals(other.body)) {
		    return false;
	    }
	    if (subject == null) {
		    if (other.subject != null) {
			    return false;
		    }
	    } else if (!subject.equals(other.subject)) {
		    return false;
	    }
	    if (type != other.type) {
		    return false;
	    }
	    return true;
    }
	
}
